<?php
define('ERROR_NO_SERVICE',				-101);
define('ERROR_NO_FUNCTION',				-102);
define('ERROR_SECURITY_REQUEST',		-103);

define('SUCCESS', 						1);

define('PROCESS_TYPE_BINARY',			1000);
define('PROCESS_TYPE_SECURITY_BINARY',	1001);

include dirname(__FILE__) . '/process/IRemotingProcess.php';

/**
 * PHP 远程调用服务类
 * 
 */
class PHPRemoting {
	var $config;
	var $process;
	
	function PHPRemoting($config = array('type' => PROCESS_TYPE_BINARY, 'path' => 'services')) {
		$this->config = $config;
	}
	
	/**
	 * 提供远程调用服务
	 *
	 */
	function service() {
		switch($this->config['type']) {
			case PROCESS_TYPE_BINARY:
			{
				include dirname(__FILE__) . '/process/BinaryProcess.php';
				
				$this->process = new BinaryProcess();
				break;
			}
			case PROCESS_TYPE_SECURITY_BINARY:
			{
				include dirname(__FILE__) . '/process/SecurityBinaryProcess.php';
				$this->process = new SecurityBinaryProcess();
				break;
			}
		}
		echo $this->process->execute($this);
	}
	
	/**
	 * 调用服务器端方法
	 *
	 * @param string $class
	 * @param string $method
	 * @param mixes $params
	 * @return mixed
	 */
	function call($class, $method, $params){
		$file = dirname(__FILE__) . '/' . $this->config['path'] . '/' . $class . '.php';
		$return = array();
		
		if(is_file($file)){
			include $file;
			
			if(class_exists($class)) {
				$action = new $class();
				if(method_exists($action, $method)) {
					$return = array(SUCCESS, call_user_method_array($method, $action, $params));
				} else {
					$return = array(ERROR_NO_FUNCTION, $class, $method);
				}
			} else {
				$return = array(ERROR_NO_SERVICE, $class, $method);
			}
		} else {
			$return = array(ERROR_NO_SERVICE, $class, $method);
		}
		
		return $return;
	}
}
?>